package com.dg11185.nearshop.app;

import android.app.Application;
import android.content.Context;

import com.baidu.mapapi.SDKInitializer;
import com.dg11185.libs.app.LibApplication;
import com.dg11185.libs.imageloader.cache.memory.impl.LRULimitedMemoryCache;
import com.dg11185.libs.imageloader.core.ImageLoader;
import com.dg11185.libs.imageloader.core.ImageLoaderConfiguration;
import com.dg11185.libs.imageloader.core.assist.QueueProcessingType;
import com.dg11185.nearshop.baidumap.MyMapLocation;
import com.dg11185.nearshop.net.response.LoginResponse;
import com.wlper.lib.uitls.LogUtils;

//import com.umeng.update.UmengUpdateAgent;

public class MyApplication extends Application {

    private static MyApplication instance;

    private static Context context;

    public static LoginResponse mLoginResponse = null;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        context = getApplicationContext();
        LibApplication.init(this);
        // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
        SDKInitializer.initialize(this);
        initImageLoader(getApplicationContext());
        LogUtils.switchLog(false);
        //初始化定位
        new MyMapLocation(this, null).start();
    }

    public static Context getContext() {
        return context;
    }

    public static MyApplication getInstance() {
        return instance;
    }

    public static void setUser(LoginResponse user) {
        mLoginResponse = user;
    }

    public static LoginResponse getUser() {
        return mLoginResponse;
    }

    /** 以下是图片加载功能初始化，设置加载过程的相关参数 */
    public void initImageLoader(Context context) {
        int memorySize = (int) (Runtime.getRuntime().maxMemory() / 10);
        if (memorySize > 10 * 1024 * 1024) {
            memorySize = 10 * 1024 * 0124;
        }
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                context)
                .memoryCacheExtraOptions(480, 800)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .threadPoolSize(5)
                .denyCacheImageMultipleSizesInMemory()
                .tasksProcessingOrder(QueueProcessingType.FIFO)
                .memoryCache(
                        new LRULimitedMemoryCache((int) Runtime.getRuntime()
                                .maxMemory() / 10))
                .discCacheSize(15 * 1024 * 1024) /**
                 * this will be called by
                 * {@link DefaultConfigurationFactory#createDiscCache}to created
                 * an {@link TotalSizeLimitedDiscCache} object
                 */
                .build();
        ImageLoader.getInstance().init(config);
    }
}
